//
//  StatusRetweetedView.swift
//  ShangHaiWeiBo
//
//  Created by 蔡杰 on 16/3/2.
//  Copyright © 2016年 蔡杰. All rights reserved.
//

import UIKit

class StatusRetweetedView: UIView {

    var viewModel: StatusViewModel?{
    
        didSet {
        
            retweetedLabel.text = viewModel?.retweetedText
            if viewModel?.status.retweeted_status == nil {
            
                retweetedLabel.snp_updateConstraints(closure: { (make) -> Void in
                    make.top.equalTo(self)
                    
                })
                picturesView.snp_updateConstraints(closure: { (make) -> Void in
                    make.top.equalTo(retweetedLabel.snp_bottom)
                    make.size.equalTo(CGSizeZero)
                    make.bottom.equalTo(self)
                })
                return
            }
            var offset:CGFloat = 0
            if viewModel?.status.retweeted_status?.pic_urls?.count > 0 {
            
                offset = CZStatusCellLayout.picturesMargin
            }
            
            retweetedLabel.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(self).offset(offset)
            
            }
            picturesView.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(retweetedLabel.snp_bottom).offset(offset)
                make.size.equalTo(CZStatusCellLayout.pictureSize)
                make.bottom.equalTo(self).offset(offset)
            }
            picturesView.pic_urls = viewModel?.status.retweeted_status?.pic_urls
//            picturesView.updateImageSize(viewModel!.picturesViewSize)
           
        }
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let retweetedLabel:UILabel = UILabel(cj_text: "作者@微博", fontSize: 14)
    private let picturesView:StatusPicturesView = StatusPicturesView()
    
}

extension StatusRetweetedView {

    private func setupUI (){
    
        backgroundColor = UIColor.cz_colorWithHex(0xF2F2F2)
        addSubview(retweetedLabel)
        addSubview(picturesView)
        //自动布局
        let layot = CZStatusCellLayout
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(layot.margin)
            make.top.equalTo(self).offset(layot.margin)
            make.right.equalTo(self).offset(-layot.margin)
        }
        picturesView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(layot.margin)
            make.left.equalTo(retweetedLabel)
            make.size.equalTo(layot.pictureSize)
            make.bottom.equalTo(self).offset(-layot.margin)
            
        }
        
        
    }
}